package com.suguowen.sblog.service;

import com.suguowen.sblog.bean.*;
import com.suguowen.sblog.bean.vo.ArticledetailVO;
import com.suguowen.sblog.mapper.ArticleMapper;
import com.suguowen.sblog.mapper.Article_detailMapper;
import com.suguowen.sblog.mapper.DIYMapper;
import com.suguowen.sblog.util.MyFileUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.nio.cs.ext.MacDingbat;

import java.io.*;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleService {

    @Autowired
    DIYMapper diyMapper;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    Article_detailMapper article_detailMapper;

    static final String FILE_LOCATION = "src/main/resources/static/editor.md-master/examples/test.md";

    // 查看文章详情
    public ArticledetailVO readArticle(int articleId) {
        // 更新阅读量
        articleMapper.updateArticleReadingQuantity(articleId);
        // 查询文章详情
        ArticledetailVO vo = diyMapper.selectArticledetailByArtId(articleId);
        return vo;
    }

    // 查看修改文章所需信息
    public Map<String,Object> modifyArt(int id){
        Article article = articleMapper.selectByPrimaryKey(id);
        String title = article.getTitle();
        String content = article_detailMapper.selectByArtId(id);
        Integer tag_id = article.getTagId();
        HashMap<String,Object> map = new HashMap<>();
        map.put("title",title);
        // 修改markdown渲染页面的内容为数据库拿到的md内容
        //MyFileUtil.setContent2File(FILE_LOCATION,content);
        map.put("content",content);
        map.put("tagId",tag_id);
        return map;
    }
    // 文章保存
    @Transactional
    public void saveArt(int articleId, String title, String html, String markdown, Timestamp modifyTime, Integer tagId) {
        // article
        Article article = new Article();
        if ("null".equals(tagId)){
            article.setTagId(999);// 999代表未分类
        }else{
            article.setTagId(tagId);
        }
        article.setArtId(articleId);
        article.setTitle(title);
        article.setUpdateTime(modifyTime);
        article.setSummary(markdown.split("\n")[0]);
        articleMapper.updateByPrimaryKeySelective(article);

        // article_detail
        Article_detailWithBLOBs article_detail = new Article_detailWithBLOBs();
        article_detail.setHtmlContent(html);
        article_detail.setMdContent(markdown);
        Article_detailExample article_detailExample = new Article_detailExample();
        Article_detailExample.Criteria criteria = article_detailExample.createCriteria();
        criteria.andArtIdEqualTo(articleId);
        article_detailMapper.updateByExampleSelective(article_detail,article_detailExample);
    }
    // 文章删除
    @Transactional
    public void deleArt(int articleId) {

        // article_detail
        Article_detailExample article_detailExample = new Article_detailExample();
        Article_detailExample.Criteria criteria = article_detailExample.createCriteria();
        criteria.andArtIdEqualTo(articleId);
        article_detailMapper.deleteByExample(article_detailExample);

        // article
        articleMapper.deleteByPrimaryKey(articleId);
    }
    // 创建文章
    @Transactional
    public void createArt(String title, String html, String markdown, Timestamp nowtime, Integer tagId, Integer userId) {
        // article
        int art_id = insertArtBase(title,userId,nowtime,nowtime,tagId);
        // article_detail
        insertArt_detail(art_id,html,markdown);
    }

    /**
     * 插入art（only not null）
     * return 自增长的article.id
     */
    public int insertArtBase(String title, Integer userId, Timestamp createTime, Timestamp updateTime, Integer tagId){
        // article
        Article article = new Article();

        article.setTitle(title);
        article.setCreateTime(createTime);
        article.setUpdateTime(updateTime);
        article.setUserId(userId);
        article.setTagId(tagId);

        articleMapper.insertArtReturnArtId(article);
        return article.getArtId();
    }

    public void insertArt_detail(Integer artId, String html, String md){
        Article_detailWithBLOBs article_detail = new Article_detailWithBLOBs();

        article_detail.setArtId(artId);
        article_detail.setHtmlContent(html);
        article_detail.setMdContent(md);

        article_detailMapper.insert(article_detail);
    }

}
